package com.yq.leetcodeOld.tree;

/**
 * @ClassName Solution108
 * @Description TODO
 * @Author yaoqiang
 * @Date 2022/2/15 9:50
 * @Version 1.0
 */
public class Solution108 {
    public static void main(String[] args) {
        //int[] nums =new int[-10,-3,0,5,9];
        //进来执行helper(nums,0,nums.length-1)
         /**
          里面判断l比r小返回空
          计算mid=(l+r)/2
          声明一个new TreeNode(nums[mid])
          root.left=helper(nums,0,mid-1);
          root.right=helper(nums,mid+1,right)
          return root
          * */
    }
    public TreeNode sortedArrayToBST(int[] nums) {
        return helper(nums, 0, nums.length - 1);
    }

    public TreeNode helper(int[] nums, int left, int right) {
        if (left > right) {
            return null;
        }

        // 总是选择中间位置左边的数字作为根节点
        int mid = (left + right) / 2;

        TreeNode root = new TreeNode(nums[mid]);
//        root.left = helper(nums, left, mid - 1);
//        root.right = helper(nums, mid + 1, right);
        return root;
    }
}
